//
//  SpeedViewController.m
//  DayDayRun
//
//  Created by 卫建 on 16/1/9.
//  Copyright © 2016年 卫建. All rights reserved.
//

#import "SpeedViewController.h"
//#import <AVFoundation/AVFoundation.h>
#import "MusicTool.h"
#pragma mark--------设置一个枚举类型,描述播放的几种状态
typedef enum : NSUInteger{
    playingStatus = 100,
    stopingStatus,
    nonstatus,
}musicStatus;

@interface SpeedViewController ()

//gif图片
@property (strong, nonatomic) IBOutlet UIWebView *gifImageView;

//旋转的图片
@property (strong, nonatomic) IBOutlet UIImageView *roteImageView;

@property (strong, nonatomic) IBOutlet UIButton *playAndStopButton;

//设置定时器
@property(nonatomic,strong)NSTimer *timer;

//用一个数组来保存所有的音乐文件
@property(nonatomic,strong)NSArray *songs;

//用一个int型的属性来记录当前的索引
@property(nonatomic,assign)int currentIndex;

@end

@implementation SpeedViewController

#pragma mark-------懒加载
-(NSArray *)songs
{
    if (_songs == nil)
    {
        self.songs = @[@"Hans Zimmer-Rock House Jail.mp3"];
    }
    return _songs;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //设置返回按钮
    [self setLeftItem];
    
    self.roteImageView.clipsToBounds = YES;
    self.roteImageView.layer.cornerRadius = 50;
 
    NSData *gif = [NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"music" ofType:@"gif"]];
    
    self.gifImageView.userInteractionEnabled = NO;
    [self.gifImageView loadData:gif MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
    
    self.playAndStopButton.tag = playingStatus;
    
}

#pragma mark------设置返回按钮
-(void)setLeftItem
{
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(leftItemAction:)];
    self.navigationItem.leftBarButtonItem = leftItem;
}

#pragma mark------点击返回按钮的响应方法
-(void)leftItemAction:(UIBarButtonItem *)sender
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}


#pragma mark-------设置播放暂停按钮
- (IBAction)playAndStopButtonDidClicked:(id)sender
{
    
    if (self.playAndStopButton.tag == playingStatus)
    {
        self.playAndStopButton.tag = stopingStatus;
        [MusicTool playMusic:self.songs[self.currentIndex]];
        
        //开始转圈
        self.timer.fireDate = [NSDate distantFuture];
        
        [self reseMusicInfo];
        
    }
    else
    {
        self.playAndStopButton.tag = playingStatus;
        //暂停播放
        [MusicTool pauseMusic:self.songs[self.currentIndex]];
        
        //暂停转圈
        self.timer.fireDate = [NSDate distantPast];
    }
    
    self.roteImageView.transform = CGAffineTransformRotate(self.roteImageView.transform, M_PI / 60);
    
 /*
    //音频文件的url路径
    NSURL *url=[[NSBundle mainBundle]URLForResource:@"ldh.mp3" withExtension:Nil];
    
    //2.创建播放器（注意：一个AVAudioPlayer只能播放一个url）
    self.player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
    
    //3.缓冲
    [self.player prepareToPlay];
    
    //4.播放
    [self.player play];
  */
    
}


#pragma mark-------重置歌曲信息
-(void)reseMusicInfo
{
    //写这个线程的意思就是为了让不用在切换歌曲时移除定时器
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        //初始化定时器  //设置时间的时候不能小于0.1，小于0.1的话系统默认使用0.1
        //方法需要设置歌词更新，还有⬇️面的滚动条
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(circleRound) userInfo:nil repeats:YES];
        
        //把time加入到运行时
        [[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];
        /*runLoop*/
        //主线程的runLoop默认是开启的,所以主线程永远不会结束
        //子线程的runLoop默认是关闭的,所以只要子线程的任务完成之后,子线程就会销毁
        
    });
    
    //立即开启定时器
    self.timer.fireDate = [NSDate distantPast];

}

-(void)circleRound
{
    if (self.playAndStopButton.tag == stopingStatus)
    {
        
        self.roteImageView.transform = CGAffineTransformRotate(self.roteImageView.transform, M_PI / 60);
        
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


/*

//#pragma mark-懒加载
//-(AVAudioPlayer *)player
//{
//    if (_player==Nil) {
//        
////        //1.音频文件的url路径
////        NSURL *url=[[NSBundle mainBundle]URLForResource:@"/Users/weijian/Desktop/ldh.mp3" withExtension:Nil];
////        
////        //2.创建播放器（注意：一个AVAudioPlayer只能播放一个url）
////        self.player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
////        
////        //3.缓冲
////        [self.player prepareToPlay];
//        
//        _player = [[AVAudioPlayer alloc]init];
//        
//    }
//    return _player;
//}

*/

/*
//-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//{
//    
//    //1.音频文件的url路径
//    NSURL *url=[[NSBundle mainBundle]URLForResource:@"/Users/weijian/Desktop/ldh.mp3" withExtension:Nil];
//    
//    //2.创建播放器（注意：一个AVAudioPlayer只能播放一个url）
//    self.audioplayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
//    
//    //3.缓冲
//    [self.audioplayer prepareToPlay];
//    
//    //4.播放
//    [self.audioplayer play];
//}

*/

@end
